using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace MotaWeb.Controllers
{
    public class BiographyController : MotaControllerBase
    {
        //
        // GET: /Biography/

        public ActionResult Index()
        {
            return View(Repository.FindBiographies());
        }

        //
        // GET: /Biography/SixWordBiographies

        public ActionResult SixWordBiographies()
        {
            return View(Repository.FindBiographies(true));
        }

        //
        // GET: /Biography/Details/5

        public ActionResult Details(int id)
        {
            return View(Repository.FindBiography(id));
        }

        //
        // GET: /Biography/Details/5

        public ActionResult DetailsForUser(string userName)
        {
            var biography = Repository.FindBiography(userName);
            if (biography == null)
            {
                return RedirectToAction("Create");
            }
            return View(biography);
        }

        //
        // GET: /Biography/Create

        [Authorize(Roles = "Member, Administrator")]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Biography/Create
        [Authorize(Roles = "Member, Administrator")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                string message = collection["BiographyText"];
                string userName = collection["UserName"];
                Repository.CreateBiography(message, userName, false);

                return RedirectToAction("Index", "Home");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Biography/Pending

        public ActionResult PendingBiographies()
        {
            return View(Repository.FindPendingBiographies());
        }

        //
        // GET: /Biography/Eit/5
        [Authorize(Roles = "Member, Administrator")]
        public ActionResult Edit(int id)
        {
            return View(Repository.FindBiography(id));
        }

        //
        // POST: /Biography/Edit/5

        [Authorize(Roles = "Member, Administrator")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }
            try
            {
                string message = collection["BiographyText"];
                bool isApproved = false;

                if (User.IsInRole("Administrator"))
                {
                    isApproved = (collection.GetValues("IsApproved").Length > 1);

                }

                Repository.EditBiography(id, message, isApproved);

                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                return View();
            }
        }
    }
}
